package com.iscc.fake.dao.Impl;

import com.iscc.fake.dao.ProductDAO;
import com.iscc.fake.domain.Product;
import com.iscc.fake.util.DataSourceUtile;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProductDAOImpl implements ProductDAO {
    @Override
    public int add( Product product) {
        int result=-1;
        Connection conn= null;
        PreparedStatement ps = null;
        String sql ="INSERT INTO product(pname,PRICE,sallery,TYPE,spec,describes,picture,src) VALUES (?,?,?,?,?,?,?,?)";
        try {
            conn = DataSourceUtile.getConnection();
            ps=conn.prepareStatement(sql);
            ps.setString(1,product.getPname());
            ps.setDouble(2,product.getPrice());
            ps.setDouble(3,product.getSallery());
            ps.setString(4,product.getType());
            ps.setString(5,product.getSpec());
            ps.setString(6,product.getDescribes());
            ps.setString(7,product.getPicture());
            ps.setString(8,product.getSrc());
            result =ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DataSourceUtile.closeReource(ps, conn);
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> queryAllProduct(String name) {
        List<Map<String, Object>> list = new ArrayList<>();
        String sql = "SELECT * FROM product ORDER BY pno ASC ";
        Connection conn= null;
        PreparedStatement ps = null;
        try {
            conn = DataSourceUtile.getConnection();
            PreparedStatement psmt = conn.prepareStatement(sql);
            ResultSet rs = psmt.executeQuery();
            while (rs.next()) {
                Map<String, Object> map = new HashMap<>();
                map.put("pno", rs.getInt(1));
                map.put("pname", rs.getString(2));
                map.put("type", rs.getString(3));
                map.put("price", rs.getDouble(4));
                map.put("stock", rs.getInt(5));
                map.put("sallery", rs.getDouble(6));
                map.put("spec", rs.getString(7));
                map.put("picture", rs.getString(8));
                map.put("describes", rs.getString(9));
                map.put("src", rs.getString(10));
                list.add(map);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DataSourceUtile.closeReource(ps,conn);
        }

        return list;
    }
    @Override
    public Product queryByid(int id) {
        Product product =null;
        String sql = "SELECT * FROM product where pno =?";
        Connection conn= null;
        PreparedStatement ps = null;

        try {
            product =new Product();
            conn = DataSourceUtile.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                product.setPno(rs.getInt(1));
                product.setPname(rs.getString(2));
                product.setType(rs.getString(3));
                product.setPrice(rs.getDouble(4));
                product.setStock(rs.getInt(5));
                product.setSallery(rs.getDouble(6));
                product.setSpec(rs.getString(7));
                product.setPicture(rs.getString(8));
                product.setDescribes(rs.getString(9));
                product.setSrc(rs.getString(10));
            }
         }catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DataSourceUtile.closeReource(ps,conn);
        }
        return product;
    }
    @Override
    public List<Map<String, Object>> querydetail(String name) {
        List<Map<String, Object>> list = new ArrayList<>();
        String sql = " select distinct f.purno,date,pprice,text,f.status,uname from form f,user u,product p,detail d  where u.uno = f.uno and p.pno = d.pno and d.purno = f.purno;";
        try {
            Connection conn = DataSourceUtile.getConnection();
            PreparedStatement psmt = conn.prepareStatement(sql);
            if (name != null && !"".equals(name)) {
                psmt.setString(1, name);
            }

            ResultSet rs = psmt.executeQuery();
            while (rs.next()) {
                Map<String, Object> map = new HashMap<>();
                map.put("purno", rs.getInt(1));
                map.put("date", rs.getString(2));
                map.put("pprice", rs.getDouble(3));
                map.put("text", rs.getString(4));
                map.put("status", rs.getInt(5));
                map.put("uname", rs.getString(6));
                list.add(map);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
        }

        return list;
    }

    @Override
    public List<Map<String, Object>> queryform(String name) {
        List<Map<String, Object>> list = new ArrayList<>();
        String sql = " select picture,pname,COUNT,price,u.uname,address,\"后天\" from product p,user u,address a,form f,detail d where u.uno = f.uno and p.pno = d.pno and d.purno = f.purno and f.addno=a.addno ";
        System.out.println("sql = " + sql);
        try {
            Connection conn = DataSourceUtile.getConnection();
            PreparedStatement psmt = conn.prepareStatement(sql);
            if (name != null && !"".equals(name)) {
                psmt.setString(1, name);
            }
            ResultSet rs = psmt.executeQuery();
            while (rs.next()) {
                Map<String, Object> map = new HashMap<>();
                map.put("picture", rs.getString(1));
                map.put("pname", rs.getString(2));
                map.put("count", rs.getInt(3));
                map.put("price", rs.getDouble(4));
                map.put("uname", rs.getString(5));
                map.put("address", rs.getString(6));
                map.put("day", rs.getString(7));
                list.add(map);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
        }

        return list;
    }
    @Override
    public int getNewId() {
        return 0;
    }

    @Override
    public int add(Object o) {
        return 0;
    }

    @Override
    public int update(Object o) {
        return 0;
    }

    @Override
    public int del(int id) {
        return 0;
    }



    @Override
    public List queryAll() {
        return null;
    }
}
